/*
 * Copyright 2010 Eligio Colutta
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package com.google.code.p2pxmppclient.executers.amule;


/**
 * 
 * @author eliosh
 * @version $Rev$
 * @lastautorcommit $Author$
 * @lastdatecommit $Date$
 */
public class ShowDlAmulecmdResponseTranslator implements AmulecmdResponseTranslator {
	@Override
	public String translate(String[] lines) {
		StringBuffer sb = new StringBuffer();
		// first line blank, so we have a readable message :-)
		sb.append("\n");
		
		int lineNum = 0;
		try{
			for(int k=lineNum; k<lines.length; k++){
				String s = lines[k];
				if(s.length()<5){
					continue;
				}
				if(s.substring(0, 3).equals(" > ")){
					if (k%2==0){	
						String[] first_row = s.split(" ");
						String hash = first_row[2];
						String fileName = first_row[3];
						s = lines[k+1];
						String[] second_row = s.substring(5).split(" - ");
						String perc_sources = second_row[0];
						String status = second_row[1];
						String speed = "";
						if(second_row.length==5){
							speed = second_row[4];						
						}
						sb.append(hash + " " + fileName + " " + perc_sources + " " + status + " " + speed + "\n");
						k++;
					}
				}
			}
		}catch(Exception ex){
			sb.append(ex.getLocalizedMessage());
		}
		return sb.toString();

	}

}
